package com.hadoop.mr.mapper;

import com.hadoop.mr.writable.ClientWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * Created by Administrator on 2019/7/11.
 */
public class ClientCountMapper extends Mapper<LongWritable, Text, Text, ClientWritable> {

    private static Logger logger = LoggerFactory.getLogger(ClientCountMapper.class);

    @Override
    protected void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {
        // 将一行的内容转成 string
        try {
            String line = value.toString();
            // 用空格切割字段
            String[] fileds = line.split(";");
            String goodsName = fileds[0];
            String client = fileds[4];
            String goodsType = fileds[7];
            Integer goodsCount = 1;
            context.write(new Text(goodsName + ";" + client), new ClientWritable(goodsName + ";" + client + ";" + goodsType, goodsName, goodsType, client, goodsCount));
        } catch (Exception e) {
            logger.error("", e);
        }
    }
}
